﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_VCATONChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FormInfo.ShowTriggerWin(Window1, txt_AuditName, hid_AuditUserNo, "/PublicModel/SelectUsers.aspx?single=1", "选择审批人");
                FormInfo.ShowTriggerWin(Window1, txt_EmpName, hid_EmpUserNo, "/PublicModel/SelectUsers.aspx?single=1", "选择申请人");


                HR_TVCATN tvc = TbsDbSession.Default.From<HR_TVCATN>().Where(HR_TVCATN._.EMPLID == CookInfo.EAdminEmpID() && HR_TVCATN._.VTYEAR == DateTime.Now.Year).ToFirst();
                if (tvc != null)
                {
                    l1.Text = "<span style=\"vertical-align:middle;\"><img src=\"/icon/error.png\" /></span> <b>您" + DateTime.Now.Year + "年剩余的年假为：" + tvc.LEFQTY.Value.ToString() + "天</b>";
                    hid_LEFQTY.Text = tvc.LEFQTY.Value.ToString();
                }
                else
                {
                    Chk_Kou.Enabled = false;
                    Chk_Kou.Text = Chk_Kou.Text + "(您还未设定年假信息)";
                }
                DataBaseHelper.BindDrp(Drp_VATYPE, EnumData.DataBaseEunm.VATYPE);
                Load_Data();
                if (!string.IsNullOrEmpty(Request["view"]))
                    Load_View();
                //EnumHelper.DrpBindDD<EnumData.VacaTionType>(Drp_VATYPE);
            }
        }

        protected void Load_View()
        {
            Button1.Hidden = false;
            Button2.Hidden = false;
            btnSave.Hidden = true;
            txt_AuditName.Readonly = true;
            txt_EDDATE.Enabled = false;
            txt_REASON.Readonly = true;
            txt_REQQTY.Readonly = true;
            txt_STDATE.Enabled = false;
            Drp_VATYPE.Readonly = true;
            l1.Hidden = true;
            Chk_Kou.Enabled = false;
        }

        protected void Load_Data()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                btnSave.Text = "修改";
                HR_VCATON vc = TbsDbSession.Default.From<HR_VCATON>().Where(HR_VCATON._.ID == int.Parse(Request["id"])).ToFirst();
                //txt_EmpID.Text = vc.EMPLID;
                txt_REASON.Text = vc.REASON;
                txt_REQQTY.Text = vc.REQQTY.Value.ToString();
                Drp_VATYPE.SelectedValue = vc.VATYPE;
                if (vc.IsUserYear.HasValue)
                {
                    Chk_Kou.Checked = vc.IsUserYear.Value;
                    if (vc.IsUserYear.Value)
                    {
                        hid_isYear.Text = "1";
                    }
                    else
                    {
                        hid_isYear.Text = "0";
                    }
                }
                txt_AuditName.Text = vc.AuditName;
                hid_AuditUserNo.Text = vc.AuditUserNo;
                txt_EDDATE.Text = vc.EndTime.Value.ToString("yyyy-MM-dd hh:mm");
                txt_STDATE.Text = vc.StartTime.Value.ToString("yyyy-MM-dd hh:mm");
                txt_EmpName.Text = vc.EMNAME;
                hid_EmpUserNo.Text = vc.EMPLID;

            }
            else
            {
                txt_EmpName.Text = CookInfo.EAdminTrueName();
                hid_EmpUserNo.Text = CookInfo.EAdminEmpID();
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool yz = true;
            if (Chk_Kou.Checked)
            {
                decimal dc = Convert.ToDecimal(txt_REQQTY.Text);
                decimal sy = Convert.ToDecimal(hid_LEFQTY.Text);
                if (dc > sy)
                    yz = false;
            }
            if (yz)
            {
                string alertMes = "申请成功！";
                HR_VCATON vc = new HR_VCATON();
                //int audituid = DataHelper.GetEmpID(txt_EmpID.Text);
                //if (audituid == 0)
                //{
                //    Alert.Show("您输入的审核人不存在！");
                //}
                //else
                //{
                int returnvalue = 0;
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    vc.ID = int.Parse(Request["id"]);
                    vc.Attach();
                    vc.REASON = txt_REASON.Text;
                    vc.REQQTY = decimal.Parse(txt_REQQTY.Text);
                    vc.VATYPE = Drp_VATYPE.SelectedValue;
                    vc.IsUserYear = Chk_Kou.Checked;
                    vc.StartTime = Convert.ToDateTime(txt_STDATE.Text);
                    vc.EndTime = Convert.ToDateTime(txt_EDDATE.Text);
                    vc.AuditName = txt_AuditName.Text;
                    vc.AuditUserNo = hid_AuditUserNo.Text;
                    vc.DepartMent = CookInfo.EAdminGroupName();
                    returnvalue = TbsDbSession.Default.Update<HR_VCATON>(vc);
                    alertMes = "修改成功！";
                }
                else
                {
                    vc.DeAttach();
                    vc.EMNAME = txt_EmpName.Text; // CookInfo.EAdminTrueName();
                    vc.EMPLID = hid_EmpUserNo.Text; //CookInfo.EAdminEmpID();
                    vc.REASON = txt_REASON.Text;
                    vc.REQQTY = decimal.Parse(txt_REQQTY.Text);
                    vc.STATUS = 0;
                    vc.VATYPE = Drp_VATYPE.SelectedValue;
                    vc.StartTime = Convert.ToDateTime(txt_STDATE.Text);
                    vc.EndTime = Convert.ToDateTime(txt_EDDATE.Text);
                    vc.AuditName = txt_AuditName.Text;
                    vc.AuditUserNo = hid_AuditUserNo.Text;
                    vc.DepartMent = CookInfo.EAdminGroupName();
                    vc.IsUserYear = Chk_Kou.Checked;
                    vc.AddTime = DateTime.Now;
                    vc.AddID = CookInfo.EAdminid();
                    vc.Adder = CookInfo.EAdminTrueName();
                    returnvalue = TbsDbSession.Default.Insert<HR_VCATON>(vc);

                    //WorkFlow_TypeTOID _type = new WorkFlow_TypeTOID();
                    //_type.DeAttach();
                    //_type.AuditID = DataHelper.GetEmpID(txt_EmpID.Text);
                    //_type.Tid = returnvalue;
                    //_type.Mtype = EnumHelper.GetEnumList<EnumData.WorkFlowType>().Find(p => p.Name == "请假流程").Value;
                    //_type.IsRead = false;
                    //_type.IsOver = false;
                    //TbsDbSession.Default.Insert<WorkFlow_TypeTOID>(_type);
                }
                if (returnvalue > 0)
                    Alert.Show(alertMes, "提示", ActiveWindow.GetHideRefreshReference());
            }
            else
                Alert.Show("您剩余的年假天数不够！");
            // }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request["id"]);
            HR_VCATON vc = new HR_VCATON();
            vc.ID = id;
            vc.Attach();
            vc.STATUS = 1;
            TbsDbSession.Default.Update<HR_VCATON>(vc);
            if (hid_isYear.Text == "1")
            {
                HR_TVCATN tvc = TbsDbSession.Default.From<HR_TVCATN>().Where(HR_TVCATN._.EMPLID == hid_EmpUserNo.Text && HR_TVCATN._.VTYEAR == DateTime.Now.Year).ToFirst();
                if (tvc != null)
                {
                    HR_TVCATN tv = new HR_TVCATN();
                    tv.ID = tvc.ID;
                    tv.Attach();
                    tv.LEFQTY = tvc.LEFQTY.Value - Convert.ToDecimal(txt_REQQTY.Text);
                    tv.COMQTY = Convert.ToDecimal(txt_REQQTY.Text);
                    TbsDbSession.Default.Update<HR_TVCATN>(tv);
                }
            }
            Alert.Show("审批完成", "提示", ActiveWindow.GetHideRefreshReference());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request["id"]);
            HR_VCATON vc = new HR_VCATON();
            vc.ID = id;
            vc.Attach();
            vc.STATUS = 2;
            TbsDbSession.Default.Update<HR_VCATON>(vc);
            Alert.Show("审批完成", "提示", ActiveWindow.GetHideRefreshReference());
        }

        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            Alert.Show("12");
        }

        protected void txt_EmpName_TextChanged(object sender, EventArgs e)
        {
            HR_EMP he = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.ID == hid_EmpUserNo.Text.Replace(",", "")).ToFirst();
            if (he != null)
            {
                HR_TVCATN tvc = TbsDbSession.Default.From<HR_TVCATN>().Where(HR_TVCATN._.EMPLID == he.EMPLID && HR_TVCATN._.VTYEAR == DateTime.Now.Year).ToFirst();
                if (tvc != null)
                {
                    Chk_Kou.Enabled = true;
                    Chk_Kou.Text = "扣年假";
                    l1.Text = "<span style=\"vertical-align:middle;\"><img src=\"/icon/error.png\" /></span> <b>您" + DateTime.Now.Year + "年剩余的年假为：" + tvc.LEFQTY.Value.ToString() + "天</b>";
                    hid_LEFQTY.Text = tvc.LEFQTY.Value.ToString();
                }
                else
                {
                    Chk_Kou.Enabled = false;
                    Chk_Kou.Text = Chk_Kou.Text + "(您还未设定年假信息)";
                }
            }
        }
    }
}